'use strict'


const { db_username, db_pwd, db_host, db_database, db_dialect } = require('../config/db')
const { Sequelize, Op } = require('sequelize');
const fs = require('fs');

let obj = {};
let sequelize = new Sequelize(db_database, db_username, db_pwd, {
    host: db_host,
    dialect: db_dialect,
});

let filename = fs.readdirSync(__dirname)
let files = filename.filter(item => {
    return item.endsWith('.js') && item !== 'index.js'
})

files.forEach(item => {
    let modelName = item.replace('.js', '');
    let model = require(__dirname + '/' + modelName)
    let tableName = modelName.toLowerCase();
    obj[modelName] = sequelize.define(tableName, model)
})


obj.sync = async () => {
    return await sequelize.sync({ force: true })
};

obj.sequelize = sequelize;
obj.Op = Op;

module.exports = obj;